// pages/register/register.js
import {Register} from '../../../models/register'
import {check} from '../../../util/identityCheck'

const registerModel = new Register()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        sendStatus: true,
        time: 60,
        selectStatus: false,
        phoneNumber: '',
        sms: '',
        password: '',
        confirmPassword: '',
        sendCode: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if(!check())return
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    setPhoneNumber(event) {
        console.log(event.detail)
        this.setData({
            phoneNumber: event.detail
        })
    },

    setSms(event) {
        console.log(event.detail)
        this.setData({
            sms: event.detail
        })
    },

    setPassword(event) {
        console.log(event.detail)
        this.setData({
            password: event.detail
        })
    },

    setConfirmPassword(event) {
        console.log(event.detail)
        this.setData({
            confirmPassword: event.detail
        })
    },
    sendSms() {
        if (!this.data.sendStatus) {
            return
        }
        let params = {
            mobile: this.data.phoneNumber,
            sendType: 0
        }
        registerModel.GetSMSCode(params, res => {
            console.log(res)
            this.switchComputed()
            this.setData({
                sendCode: res.SMSCode,
                time: 60
            })
        })
    },

    agreeClick() {
        this.setData({
            selectStatus: !this.data.selectStatus
        })
    },

    switchComputed() {
        this.setData({
            sendStatus: false
        })
        let allTime = 60
        let computedTime = setInterval(() => {
            this.setData({
                time: allTime--
            })
            if (this.data.time === 0) {
                clearInterval(computedTime)
                this.setData({
                    sendStatus: true
                })
            }
        }, 1000)
    },

    toast(arg) {
        wx.showToast({
            title: arg,
            icon: 'none'
        })
    },

    /*
    * 注册
    * */
    confirm() {

        if (!this.data.phoneNumber) {
            this.toast('您输入的手机号为空！！')
            return
        }

        if (!this.data.sms) {
            this.toast('你输入的短信验证码为空！！')
            return
        }

        if (!this.data.password) {
            this.toast('你输入的密码为空！！')
            return
        }

        if (!this.data.selectStatus) {
            this.toast('请同意闲买用户协议哦！')
            return
        }

        if (this.data.password !== this.data.confirmPassword) {
            this.toast('两次输入的密码不一致！！')
            return
        }

        let params = {
            username: this.data.phoneNumber,
            password: this.data.password,
            distribution_yard: wx.getStorageSync('otherCode') ? wx.getStorageSync('otherCode') : '',
            code: this.data.sms,
            SendCode: this.data.sendCode
        }
        registerModel.GetUserRegister(params, res => {
            console.log(res)
            wx.showToast({
                title: '注册成功',
                icon: 'none'
            })
            wx.navigateBack()
        })
    },
    readAgree() {
        wx.navigateTo({
            url: '/pages/open-type/open-type?url=https://m.xianmaiyangsheng.com/gxxy_zc.html'
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})
